import {defineStore} from 'pinia'
import {ref,computed} from 'vue'
import {getCurrentDate, initList} from '@/utils'

export const useCalendarStore = defineStore('calendar', () => {
    // 当前日期
    const currentDate = getCurrentDate()
    // 选中日期
    const selectedDate = ref(currentDate)
    // 选中年
    const currentYear = computed({
        get: () => selectedDate.value.year,
        set: (value) => {
            selectedDate.value.year = value
        }
    })
    // 选中月
    const currentMonth = computed({
        get: () => selectedDate.value.month,
        set: (value) => {
            selectedDate.value.month = value
        }
    })
    // 选中日
    const currentDate1 = computed({
        get: () => selectedDate.value.date,
        set: (value) => {
            selectedDate.value.date = value
        }
    })

    // 上个月
    const lastMonth = () => {
        if(currentMonth.value--<=1){
            currentYear.value--
            currentMonth.value=12
        }
    }
    // 下个月
    const nextMonth = () => {
        if(currentMonth.value++>=12){
            currentYear.value++
            currentMonth.value=1
        }
    }

    // 今日获取
    const backToday = () => {
        selectedDate.value = getCurrentDate()
    }

    // 日历列表
    const list = computed(() => {
        return initList(currentYear.value, currentMonth.value)
    })

    const formatDate = computed(() => {
        return `${currentYear.value}-${currentMonth.value}-${currentDate1.value}`
    })

    return {
        currentYear,
        currentMonth,
        currentDate1,
        list,
        lastMonth,
        nextMonth,
        backToday,
        formatDate
    }
})